﻿// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Unlit/GrabPass"
{
    Properties
    {
       // _MainTex ("Texture", 2D) = "white" {}
       _DistortStrength("DistortStrength",range(0,5))=1
    }
    SubShader
    {
        ZWrite Off
        GrabPass
        {
            "_GrabTempTex"
        }
        
        Pass
        {
            Tags{
                "RenderType" = "Transparent"
                "Queue" = "Transparent+100"
            }
            CGPROGRAM
            
            
            #include "UnityCG.cginc"
            
            #pragma vertex  vert
            #pragma fragment  frag
            
            float _DistortStrength;
            sampler2D _GrabTempTex;
            float4 _GrabTempTex_ST;
         // sampler2D _GrabTexture;
         // float4 _GrabTexture_ST;
            struct v2f
            {
                float4 pos : SV_POSITION;
                float4 grabPos : TEXCOORDO;
            };
            
            v2f vert(appdata_base v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                //计算抓屏的位置，其中主要是将坐标从（-1,1）
				//转化到（0,1）空间并处理DX和GL纹理反向问题
                o.grabPos = ComputeGrabScreenPos(o.pos);
                
                return o;
            
            }
            
            fixed4 frag(v2f i):SV_Target
            {
            //画面偏移效果
            i.grabPos.x += _DistortStrength * sin(_Time.y * 10);
            i.grabPos.y += _DistortStrength * sin(_Time.y);
            
            //根据抓屏位置采样Grab贴图，tex2DProj等
			//同于tex2D(grabPos.xy/grabPos.w),采样点坐标进行了一次投影变换
            fixed4 color = tex2Dproj(_GrabTempTex,i.grabPos);
            //输出颜色的反像
            return 1-color;
            }
            
            
            ENDCG
        }
        
    }
}
